// pages/courier/courier.js 
var app = getApp()
var common = require('../../utils/common.js')
var util = require('../../utils/util.js');
var Bmob = require('../../utils/Bmob-1.6.5.min.js');
var showAlert = require('../../utils/showAlert.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    postsList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTopList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getTopList();
  },
  getTopList: function () {
    var that = this;
    that.setData({ pageIndex: 1 });

    const query = Bmob.Query("orders");
    query.order("createAt");
    query.limit(10);
    // query.skip(10 * (pageIndex - 1));
    // query.limit(10);
    query.find().then(res => {
      // 返回 isLike > 150 or isLike < 5 的值
      that.setData({ postsList: res });

      wx.stopPullDownRefresh();
      console.log(res)
    });

  },
  isAll: false,
  getPageList: function () {
    if (this.isAll) return;
    var that = this;
    var pageIndex = this.data.pageIndex;

    var dataList = this.data.postsList;

    const query = Bmob.Query("orders");
    query.order("createAt");

    query.skip(10 * (pageIndex));
    query.limit(10);
    query.find().then(res => {
      // 返回 isLike > 150 or isLike < 5 的值
      var a = new Array();
      res.forEach(function (v, index) {

        dataList.push(v);

      });

      if (res.length == 10)
        that.setData({ pageIndex: pageIndex + 1 });
      else
        that.isAll = true;
      console.log(that.data.pageIndex);
      that.setData({ postsList: dataList });

      wx.stopPullDownRefresh();
      console.log(res)
    });

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(1111);
    this.getPageList(true);
  },
  /**
 * 接单
 */
  viewDetaieTap: function (e) {
    console.log(e);
    var that = this;
    let actid = e.currentTarget.dataset.actid;
    wx.showActionSheet({
      itemList: ['收件', '派件', '退单'],
      success(res) {
        console.log(res.tapIndex);
        var status = 0;
        if (res.tapIndex == 0) {
          status = 1;

        }
        if (res.tapIndex == 1) {
          status = 2;

        }
        if (res.tapIndex == 2) {
          status = 3;

        }

        const query = Bmob.Query('orders');

        query.set('id', actid) //需要修改的objectId
        query.set('Status', status)
        query.save().then(res => {
          console.log(res)
          showAlert.showTip("操作完成");
          that.getList();
        }).catch(err => {
          console.log(err)
        })

      },
      fail(res) {
        console.log(res.errMsg)
      }
    })

    return;

    showAlert.showModal("确定接单吗", "提示!", function () {




    }, true, '我要接单');
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})